﻿Imports System.Drawing
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports Camstar.WebClient.FormsBuilder.WebForm
Imports Camstar.WebClient.WebServicesProxy.InSiteWebServices
Imports System.IO
Imports Camstar.WebClient.WebConstants
Imports BusinessRule.IBusiness
Imports BusinessRule.DTO
Imports BusinessRule.BusinessFactory
Imports BusinessRule.Common

Partial Class DealFaultBillPrintPopupNew
    Inherits System.Web.UI.Page

    Private oIBusiness As uLMSDealFaultBillIbusiness = uMESBusinessFactory.instance.GetuLMSdealFaultBillObject()
    Public strProductName As String
    Public htmlstr As String
    Public strHtmlTable As String
    Public Shared PrintType As String
    Public Shared strEName As String

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            If IsNothing(Session("PGDIDForPrint")) = False Then
                Dim para As New Dictionary(Of String, String)
                para("PGDID") = Session("PGDIDForPrint")
                If para("PGDID").ToString = String.Empty Then
                    Response.Write("<script type='text/javascript'>alert('没有要打印的排故单');window.close();</script>")
                    Return
                Else
                    Dim PGDID As String = para("PGDID")
                    strHtmlTable = String.Empty
                    iniPate(PGDID)
                End If
            Else
                Response.Write("<script type='text/javascript'>alert('没有要打印的排故单');window.close();</script>")
                Return
            End If
        End If
    End Sub

    Private Sub iniPate(ByVal PGDID As String)
        Dim strHtmlTitle As String
        Dim strHtmlTableMain As String
        Dim strHtmlTableDetails As String

        Dim dic As New Dictionary(Of String, String)
        dic.Add("DealFaultBillID", PGDID)
        Dim PR As uMESProcessResult = oIBusiness.GetPGDInfo(dic)

        strHtmlTableMain = SetTableMain(PR.DBTable)
        strHtmlTitle = SetTableTitle(Nothing)
        strHtmlTableDetails = SetTableDetail(Nothing)

        strHtmlTable = String.Format("{0}{1}{2}", strHtmlTitle, strHtmlTableMain, strHtmlTableDetails)
    End Sub

    Private Function SetTableMain(ByVal PR As DataTable) As String
        Dim dt As DataTable = PR
        Dim strId As String = Session("PGDIDForPrint")
        strEName = dt.Rows(0)("DealFaultbillname").ToString
        Dim strFamilyName As String = dt.Rows(0)("productfamilyname").ToString
        'Dim strMachineName As String = dt.Rows(0)("containername").ToString '发动机号
        'Dim strComCName As String = dt.Rows(0)("comContainername").ToString '组合件号
        Dim strWorkhours As String = dt.Rows(0)("machineworkhours").ToString '工作时间
        Dim strfaultdesc As String = dt.Rows(0)("faultdesc").ToString '故障情况

        Dim sbhtml As New StringBuilder

        sbhtml.AppendLine("<table id='Up' width='100%' border='1' cellspacing='0' cellpadding='0'> ")
        sbhtml.AppendLine("<tr>    <td   style='width:80px;'>型号</td>    <td  style='width:200px;'>" + strFamilyName + "</td> ")
        sbhtml.AppendLine(" <td rowspan='2' <span class='STYLE5'>排除故障工艺卡片</span></td> ")
        sbhtml.AppendLine("<td  style='width:80px;'>工作时间</td>    <td  style='width:200px;'>" + strWorkhours + " </td> ")
        sbhtml.AppendLine(" </tr>  <tr>    <td>机号</td>    <td>&nbsp;</td>    <td>组合件号</td>    <td>&nbsp;</td>  </tr></table> ")
        sbhtml.AppendLine("<table id='Mid' width='100%' frame=vsides cellspacing='0' cellpadding='0'> ")
        sbhtml.AppendLine("<tr>    <td colspan='5' style=' text-align:left'>故障情况:</td>      </tr> ")
        sbhtml.AppendLine(" <tr>    <td  colspan='5'>" + strfaultdesc + " </td>      </tr>  <tr>")
        sbhtml.AppendLine(" <td style='width:100px'>&nbsp;</td>    <td style='width:500px'>&nbsp;</td> ")
        sbhtml.AppendLine("<td style='width:100px'>检验员</td>    <td style='width:200px'>&nbsp;</td> ")
        sbhtml.AppendLine(" <td style='width:100px'>军代表</td>    <td style='width:200px'>&nbsp;</td>  </tr></table>")

        sbhtml.AppendLine("<table class='Down' width='100%' border='1' cellspacing='0' cellpadding='0'> ")
        sbhtml.AppendLine(" <tr>    <td  style='width:80px;'>序号</td>    <td style='width:510px;'>排除方法</td> ")
        sbhtml.AppendLine("    <td  style='width:310px;'>排除情况</td>   <td  style='width:150px;'>工人</td>")
        sbhtml.AppendLine(" <td  style='width:150px;'>检验员</td>  </tr>")

        Dim dtStep As DataTable = oIBusiness.GetPGDStepInfo(strId)
        '循环处理
        Dim RowCount As Integer = dtStep.Rows.Count

        If RowCount < 8 Then
            For i As Integer = 0 To dtStep.Rows.Count - 1
                sbhtml.AppendLine(" <tr>    <td>" + (i + 1).ToString + "</td>    <td>" + dtStep.Rows(i)("DealFaultdesc").ToString + "</td>    <td>&nbsp;</td>    <td>&nbsp;</td>    <td>&nbsp;</td>  </tr> ")
            Next

            For i As Integer = dtStep.Rows.Count To 7 - 1
                sbhtml.AppendLine(" <tr>    <td>&nbsp;</td>    <td>&nbsp;</td>    <td>&nbsp;</td>    <td>&nbsp;</td>    <td>&nbsp;</td>  </tr> ")
            Next

            sbhtml.AppendLine("</table> ")
            sbhtml.AppendLine("<table class='Bot' width='100%' border='1'; cellspacing='0' cellpadding='0'> ")
            sbhtml.AppendLine("  <tr>    <td style='width:100px'>工艺员</td>    <td style='width:200px'>&nbsp;</td>")
            sbhtml.AppendLine(" <td style='width:100px'>技术主任</td>    <td style='width:200px'>&nbsp;</td>")
            sbhtml.AppendLine(" <td style='width:100px'>检验室主任</td>    <td style='width:210px'>&nbsp;</td> ")
            sbhtml.AppendLine("<td style='width:100px'>军代表</td>    <td style='width:190px'>&nbsp;</td>  </tr></table></div> ")
        Else
            '如果有分页
            '第一页七个，第二页到第n页
            '判断剩下的共多少页
            '每页

            '第一页
            For i As Integer = 0 To 7 - 1
                sbhtml.AppendLine(" <tr>    <td>" + (i + 1).ToString + "</td>    <td>" + dtStep.Rows(i)("DealFaultdesc").ToString + "</td>    <td>&nbsp;</td>    <td>&nbsp;</td>    <td>&nbsp;</td>  </tr> ")
            Next
            sbhtml.AppendLine("</table> ")
            sbhtml.AppendLine("<table class='Bot' width='100%' border='1'; cellspacing='0' cellpadding='0'> ")
            sbhtml.AppendLine("  <tr>    <td style='width:100px'>工艺员</td>    <td style='width:200px'>&nbsp;</td>")
            sbhtml.AppendLine(" <td style='width:100px'>技术主任</td>    <td style='width:200px'>&nbsp;</td>")
            sbhtml.AppendLine(" <td style='width:100px'>检验室主任</td>    <td style='width:210px'>&nbsp;</td> ")
            sbhtml.AppendLine("<td style='width:100px'>军代表</td>    <td style='width:190px'>&nbsp;</td>  </tr></table></div> ")


            Dim leftPage As Integer = 0

            If (RowCount - 7) Mod 17 = 0 Then
                leftPage = Math.Truncate((RowCount - 7) / 17)
            Else
                leftPage = Math.Truncate((RowCount - 7) / 17) + 1
            End If

            For j As Integer = 1 To leftPage
                sbhtml.AppendLine("<div style='width: 1200px;height: 1000px;' >")
                sbhtml.AppendLine("<table class='Down' width='100%' border='1' cellspacing='0' cellpadding='0'>")
                sbhtml.AppendLine("<tr>    <td  style='width:80px;'>序号</td>    <td style='width:510px;'>排除方法</td> ")
                sbhtml.AppendLine("<td  style='width:310px;'>排除情况</td>    <td  style='width:150px;'>工人</td>    <td  style='width:150px;'>检验员</td>  </tr>")
                If j < leftPage Then
                    '不是最后一页，全部打印
                    For k As Integer = 0 To 16
                        sbhtml.AppendLine(" <tr>    <td>" + (7 + (j - 1) * 17 + (k + 1)).ToString + "</td>    <td>" + dtStep.Rows(7 + (j - 1) * 17 + (k))("DealFaultdesc").ToString + "</td>    <td>&nbsp;</td>    <td>&nbsp;</td>    <td>&nbsp;</td>  </tr> ")
                    Next

                Else
                    '最后一页
                    '如果当前数据没有打印完，正常打印，如果打印完，补空
                    For k As Integer = 0 To 16
                        If 7 + (j - 1) * 17 + (k + 1) < RowCount Then
                            sbhtml.AppendLine(" <tr>    <td>" + (7 + (j - 1) * 17 + (k + 1)).ToString + "</td>    <td>" + dtStep.Rows(7 + (j - 1) * 17 + (k))("DealFaultdesc").ToString + "</td>    <td>&nbsp;</td>    <td>&nbsp;</td>    <td>&nbsp;</td>  </tr> ")
                        Else
                            sbhtml.AppendLine(" <tr>    <td></td>    <td></td>    <td>&nbsp;</td>    <td>&nbsp;</td>    <td>&nbsp;</td>  </tr> ")
                        End If

                    Next
                End If

                sbhtml.AppendLine("</table> ")
                sbhtml.AppendLine("<table class='Bot' width='100%' border='1'; cellspacing='0' cellpadding='0'> ")
                sbhtml.AppendLine("  <tr>    <td style='width:100px'>工艺员</td>    <td style='width:200px'>&nbsp;</td>")
                sbhtml.AppendLine(" <td style='width:100px'>技术主任</td>    <td style='width:200px'>&nbsp;</td>")
                sbhtml.AppendLine(" <td style='width:100px'>检验室主任</td>    <td style='width:210px'>&nbsp;</td> ")
                sbhtml.AppendLine("<td style='width:100px'>军代表</td>    <td style='width:190px'>&nbsp;</td>  </tr></table></div> ")

            Next

        End If

        SetTableMain = sbhtml.ToString()
    End Function

    Private Function SetTableDetail(ByVal PR As uMESProcessResult) As String
        SetTableDetail = String.Empty
    End Function

    Private Function SetTableTitle(ByVal PR As uMESProcessResult) As String
        Dim sbhtml As New StringBuilder
        SetTableTitle = sbhtml.ToString
    End Function
End Class
